package com.kateba.android.bluetooth;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothClass;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.kateba.android.blueinput.Singleton;


public class BTInterface
{
  static final int L2CAP_PSM_HIDP_CTRL = 17;
  static final int L2CAP_PSM_HIDP_INTR = 19;
  private static final String LOG_TAG = "BlueInput::BTInterface";
  static final int MAX_DEVICES = 10;
  private static final String m_szCachePath = "/data/data/com.teksoftco.android.blueinput/btcache.dat";
  public int BTCount = 0;
  public BTDev[] BTDevs = new BTDev[10];
  public BTDev BTServer;
  public final BroadcastReceiver mReceiver = new BroadcastReceiver()
  {
    public void onReceive(Context paramContext, Intent paramIntent)
    {
      String str1 = paramIntent.getAction();
      String str2= null;
      String str3 = null;
      int i = 0;
      int j;
      int k = 0;
      if ("android.bluetooth.device.action.FOUND".equals(str1))
      {
        BluetoothDevice localBluetoothDevice = (BluetoothDevice)paramIntent.getParcelableExtra("android.bluetooth.device.extra.DEVICE");
        BluetoothClass localBluetoothClass = localBluetoothDevice.getBluetoothClass();
        if (localBluetoothClass.getMajorDeviceClass() == 1280)
        {
          str2 = localBluetoothDevice.getName();
          str3 = localBluetoothDevice.getAddress();
          if (localBluetoothClass.getDeviceClass() != 1408)
           // break label320;
          i = 1;
          Singleton.getInstance().Debug(0, "BlueInput::BTInterface", "(D) ASDKDiscoverBluetoothDevices (" + BTInterface.this.BTCount + "): Name(" + str2 + ") Addr(" + str3 + ") DevType:" + i + " RD not available");
          j = 0;
          k = 0;
          label169: if (k < BTInterface.this.BTCount)
            //break label326;
          label181: if (j == 0)
          {
            Singleton.getInstance().Debug("BlueInput::BTInterface", "FOUND NEW BT Dev (t=" + i + ")");
            if (BTInterface.this.BTCount >= 10)
              //break label516;
            BTInterface.this.BTDevs[BTInterface.this.BTCount] = new BTDev(str2, str3, i, null);
            BTInterface.this.BTDevs[BTInterface.this.BTCount].m_nId = BTInterface.this.BTCount;
            BTInterface.this.BTDevs[BTInterface.this.BTCount].m_bBTDEVRDValid = false;
            BTInterface localBTInterface = BTInterface.this;
            localBTInterface.BTCount = (1 + localBTInterface.BTCount);
          }
        }
      }
      while (true)
      {
        //return;
        //label320:
        	i = 0;
        //break;
        //label326: 
        	if (BTInterface.this.BTDevs[k].m_szAddress.compareTo(str3) == 0)
        {
          Singleton.getInstance().Debug(0, "BlueInput::BTInterface", "Duplicate to position(" + k + "): Name(" + BTInterface.this.BTDevs[k].m_szName + ") RD valid:" + BTInterface.this.BTDevs[k].m_bBTDEVRDValid);
          if ((str2.length() > 0) && ((BTInterface.this.BTDevs[k].m_szName.length() == 0) || (BTInterface.this.BTDevs[k].m_szName.compareTo("null") == 0)))
          {
            Singleton.getInstance().Debug(0, "BlueInput::BTInterface", "Take duplicate name:" + str2);
            BTInterface.this.BTDevs[k].m_szName = str2;
          }
          j = 1;
          //break label181;
        }
        k++;
       // break label169;
        label516: Singleton.getInstance().Debug("BlueInput::BTInterface", "ASDKDiscoverBluetoothDevices Max devices reached.");
        //continue;
        if (!"android.bluetooth.adapter.action.DISCOVERY_FINISHED".equals(str1))
         // continue;
        BTInterface.this.m_ASDKScanRunning = false;
      }
    }
  };
  private boolean m_ASDKScanRunning = false;
  public BluetoothAdapter m_BluetoothAdapter = null;

  public BTInterface()
  {
    LoadCache();
    Singleton.getInstance().Debug("BlueInput::BTInterface", "Cache done [" + this.BTCount + " devs]\nprepare BT Adapter");
    this.m_BluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    Singleton.getInstance().Debug("BlueInput::BTInterface", "Bluetooth constructor done.");
  }

  public boolean CheckBluetoothEnabled()
  {
    if (this.m_BluetoothAdapter == null);
    for (boolean bool = false; ; bool = this.m_BluetoothAdapter.isEnabled())
      return bool;
  }

  // ERROR //
  public int Connect(int paramInt)
  {
	return paramInt;
    // Byte code:
    //   0: bipush 252
    //   2: istore_2
    //   3: iload_1
    //   4: aload_0
    //   5: getfield 50	com/teksoftco/android/bluetooth/BTInterface:BTCount	I
    //   8: if_icmpge +7 -> 15
    //   11: iload_1
    //   12: ifge +8 -> 20
    //   15: bipush 255
    //   17: istore_2
    //   18: iload_2
    //   19: ireturn
    //   20: aload_0
    //   21: getfield 48	com/teksoftco/android/bluetooth/BTInterface:BTDevs	[Lcom/teksoftco/android/bluetooth/BTDev;
    //   24: iload_1
    //   25: aaload
    //   26: aload_0
    //   27: getfield 48	com/teksoftco/android/bluetooth/BTInterface:BTDevs	[Lcom/teksoftco/android/bluetooth/BTDev;
    //   30: iload_1
    //   31: aaload
    //   32: getfield 106	com/teksoftco/android/bluetooth/BTDev:m_szAddress	Ljava/lang/String;
    //   35: bipush 17
    //   37: invokestatic 112	com/teksoftco/android/bluetooth/BluetoothReflection:createL2CAPBluetoothSocket	(Ljava/lang/String;I)Landroid/bluetooth/BluetoothSocket;
    //   40: putfield 116	com/teksoftco/android/bluetooth/BTDev:m_btsckCTRL	Landroid/bluetooth/BluetoothSocket;
    //   43: aload_0
    //   44: getfield 48	com/teksoftco/android/bluetooth/BTInterface:BTDevs	[Lcom/teksoftco/android/bluetooth/BTDev;
    //   47: iload_1
    //   48: aaload
    //   49: getfield 116	com/teksoftco/android/bluetooth/BTDev:m_btsckCTRL	Landroid/bluetooth/BluetoothSocket;
    //   52: ifnull -34 -> 18
    //   55: aload_0
    //   56: getfield 48	com/teksoftco/android/bluetooth/BTInterface:BTDevs	[Lcom/teksoftco/android/bluetooth/BTDev;
    //   59: iload_1
    //   60: aaload
    //   61: getfield 116	com/teksoftco/android/bluetooth/BTDev:m_btsckCTRL	Landroid/bluetooth/BluetoothSocket;
    //   64: invokevirtual 121	android/bluetooth/BluetoothSocket:connect	()V
    //   67: invokestatic 60	com/teksoftco/android/blueinput/Singleton:getInstance	()Lcom/teksoftco/android/blueinput/Singleton;
    //   70: ldc 15
    //   72: new 62	java/lang/StringBuilder
    //   75: dup
    //   76: ldc 123
    //   78: invokespecial 67	java/lang/StringBuilder:<init>	(Ljava/lang/String;)V
    //   81: aload_0
    //   82: getfield 48	com/teksoftco/android/bluetooth/BTInterface:BTDevs	[Lcom/teksoftco/android/bluetooth/BTDev;
    //   85: iload_1
    //   86: aaload
    //   87: getfield 126	com/teksoftco/android/bluetooth/BTDev:m_szName	Ljava/lang/String;
    //   90: invokevirtual 76	java/lang/StringBuilder:append	(Ljava/lang/String;)Ljava/lang/StringBuilder;
    //   93: ldc 128
    //   95: invokevirtual 76	java/lang/StringBuilder:append	(Ljava/lang/String;)Ljava/lang/StringBuilder;
    //   98: invokevirtual 80	java/lang/StringBuilder:toString	()Ljava/lang/String;
    //   101: invokevirtual 84	com/teksoftco/android/blueinput/Singleton:Debug	(Ljava/lang/String;Ljava/lang/String;)V
    //   104: aload_0
    //   105: getfield 48	com/teksoftco/android/bluetooth/BTInterface:BTDevs	[Lcom/teksoftco/android/bluetooth/BTDev;
    //   108: iload_1
    //   109: aaload
    //   110: aload_0
    //   111: getfield 48	com/teksoftco/android/bluetooth/BTInterface:BTDevs	[Lcom/teksoftco/android/bluetooth/BTDev;
    //   114: iload_1
    //   115: aaload
    //   116: getfield 106	com/teksoftco/android/bluetooth/BTDev:m_szAddress	Ljava/lang/String;
    //   119: bipush 19
    //   121: invokestatic 112	com/teksoftco/android/bluetooth/BluetoothReflection:createL2CAPBluetoothSocket	(Ljava/lang/String;I)Landroid/bluetooth/BluetoothSocket;
    //   124: putfield 131	com/teksoftco/android/bluetooth/BTDev:m_btsckINTR	Landroid/bluetooth/BluetoothSocket;
    //   127: aload_0
    //   128: getfield 48	com/teksoftco/android/bluetooth/BTInterface:BTDevs	[Lcom/teksoftco/android/bluetooth/BTDev;
    //   131: iload_1
    //   132: aaload
    //   133: getfield 131	com/teksoftco/android/bluetooth/BTDev:m_btsckINTR	Landroid/bluetooth/BluetoothSocket;
    //   136: ifnull -118 -> 18
    //   139: aload_0
    //   140: getfield 48	com/teksoftco/android/bluetooth/BTInterface:BTDevs	[Lcom/teksoftco/android/bluetooth/BTDev;
    //   143: iload_1
    //   144: aaload
    //   145: getfield 131	com/teksoftco/android/bluetooth/BTDev:m_btsckINTR	Landroid/bluetooth/BluetoothSocket;
    //   148: invokevirtual 121	android/bluetooth/BluetoothSocket:connect	()V
    //   151: invokestatic 60	com/teksoftco/android/blueinput/Singleton:getInstance	()Lcom/teksoftco/android/blueinput/Singleton;
    //   154: ldc 15
    //   156: new 62	java/lang/StringBuilder
    //   159: dup
    //   160: ldc 133
    //   162: invokespecial 67	java/lang/StringBuilder:<init>	(Ljava/lang/String;)V
    //   165: aload_0
    //   166: getfield 48	com/teksoftco/android/bluetooth/BTInterface:BTDevs	[Lcom/teksoftco/android/bluetooth/BTDev;
    //   169: iload_1
    //   170: aaload
    //   171: getfield 126	com/teksoftco/android/bluetooth/BTDev:m_szName	Ljava/lang/String;
    //   174: invokevirtual 76	java/lang/StringBuilder:append	(Ljava/lang/String;)Ljava/lang/StringBuilder;
    //   177: ldc 128
    //   179: invokevirtual 76	java/lang/StringBuilder:append	(Ljava/lang/String;)Ljava/lang/StringBuilder;
    //   182: invokevirtual 80	java/lang/StringBuilder:toString	()Ljava/lang/String;
    //   185: invokevirtual 84	com/teksoftco/android/blueinput/Singleton:Debug	(Ljava/lang/String;Ljava/lang/String;)V
    //   188: iconst_0
    //   189: istore_2
    //   190: goto -172 -> 18
    //   193: astore_3
    //   194: aload_0
    //   195: getfield 48	com/teksoftco/android/bluetooth/BTInterface:BTDevs	[Lcom/teksoftco/android/bluetooth/BTDev;
    //   198: iload_1
    //   199: aaload
    //   200: getfield 116	com/teksoftco/android/bluetooth/BTDev:m_btsckCTRL	Landroid/bluetooth/BluetoothSocket;
    //   203: invokevirtual 136	android/bluetooth/BluetoothSocket:close	()V
    //   206: bipush 254
    //   208: istore_2
    //   209: goto -191 -> 18
    //   212: astore 5
    //   214: aload_0
    //   215: getfield 48	com/teksoftco/android/bluetooth/BTInterface:BTDevs	[Lcom/teksoftco/android/bluetooth/BTDev;
    //   218: iload_1
    //   219: aaload
    //   220: getfield 131	com/teksoftco/android/bluetooth/BTDev:m_btsckINTR	Landroid/bluetooth/BluetoothSocket;
    //   223: invokevirtual 136	android/bluetooth/BluetoothSocket:close	()V
    //   226: bipush 253
    //   228: istore_2
    //   229: goto -211 -> 18
    //   232: astore 6
    //   234: goto -8 -> 226
    //   237: astore 4
    //   239: goto -33 -> 206
    //
    // Exception table:
    //   from	to	target	type
    //   55	67	193	java/io/IOException
    //   139	151	212	java/io/IOException
    //   214	226	232	java/io/IOException
    //   194	206	237	java/io/IOException
  }

  public int Disconnect(int paramInt)
  {
    Singleton.getInstance().Debug("BlueInput::BTInterface", "Disconnect call for " + paramInt);
    int i;
    if ((paramInt >= this.BTCount) || (paramInt < 0))
      i = -1;
    while (true)
    {
      //return i;
      Singleton.getInstance().Debug("BlueInput::BTInterface", "Go for ASDK sockets");
      if (this.BTDevs[paramInt].m_btsckCTRL != null);
      try
      {
        this.BTDevs[paramInt].m_btsckCTRL.close();
        this.BTDevs[paramInt].m_btsckCTRL = null;
        if (this.BTDevs[paramInt].m_btsckINTR == null);
      }
      catch (IOException localIOException2)
      {
        try
        {
          this.BTDevs[paramInt].m_btsckINTR.close();
          this.BTDevs[paramInt].m_btsckINTR = null;
          Singleton.getInstance().Debug("BlueInput::BTInterface", "Disconnect done.");
          i = 0;
        //  continue;
          localIOException2 = localIOException2;
          Singleton.getInstance().Debug("BlueInput::BTInterface", "Disconnect CTRL ERROR: " + localIOException2.getMessage());
        }
        catch (IOException localIOException1)
        {
          while (true)
            Singleton.getInstance().Debug("BlueInput::BTInterface", "Disconnect INTR ERROR: " + localIOException1.getMessage());
        }
      }
    }
  }

  public int DiscoverBluetoothDevices()
  {
    int i = -1;
    Singleton.getInstance().Debug("BlueInput::BTInterface", "ASDKDiscoverBluetoothDevices called.");
    if (this.m_BluetoothAdapter == null);
    while (true)
    {
      //return i;
      if (this.m_BluetoothAdapter.isDiscovering())
        this.m_BluetoothAdapter.cancelDiscovery();
      int j = this.BTCount;
      if (!this.m_BluetoothAdapter.startDiscovery())
        continue;
      this.m_ASDKScanRunning = true;
      while (this.m_ASDKScanRunning);
      SaveCache();
      i = this.BTCount - j;
    }
  }

  int LoadCache()
  {
    int i = 0;
    try
    {
      ObjectInputStream localObjectInputStream = new ObjectInputStream(new FileInputStream("/data/data/com.teksoftco.android.blueinput/btcache.dat"));
      this.BTCount = localObjectInputStream.readInt();
      Singleton.getInstance().Debug("BlueInput::BTInterface", "LoadCache: " + this.BTCount + " devs.");
      int j = 0;
      if (j >= this.BTCount)
      {
        localObjectInputStream.close();
      }
      else
      {
        int k = localObjectInputStream.readInt();
        byte[] arrayOfByte1 = new byte[k];
        localObjectInputStream.read(arrayOfByte1, 0, k);
        int m = localObjectInputStream.readInt();
        byte[] arrayOfByte2 = new byte[m];
        localObjectInputStream.read(arrayOfByte2, 0, m);
        int n = localObjectInputStream.readInt();
        int i1 = localObjectInputStream.readInt();
        byte[] arrayOfByte3 = new byte[i1];
        localObjectInputStream.read(arrayOfByte3, 0, i1);
        this.BTDevs[j] = new BTDev(new String(arrayOfByte1), new String(arrayOfByte2), n, arrayOfByte3);
        this.BTDevs[j].m_nId = j;
        this.BTDevs[j].m_bBTDEVRDValid = false;
        if (arrayOfByte3.length > 0)
        {
          Singleton.getInstance().Debug("BlueInput::BTInterface", "Call parser for " + this.BTDevs[j].m_szName);
          this.BTDevs[j].HidParserObj.ParseRD(arrayOfByte3, arrayOfByte3.length);
          this.BTDevs[j].m_bBTDEVRDValid = true;
        }
        while (true)
        {
          Singleton.getInstance().Debug("BlueInput::BTInterface", "LoadCache: dev=" + j + " [" + this.BTDevs[j].m_szName + "][" + this.BTDevs[j].m_szAddress + "] type=" + n + " RDValid=" + this.BTDevs[j].m_bBTDEVRDValid);
          j++;
        //  break;
          Singleton.getInstance().Debug("BlueInput::BTInterface", "Invalid RD for " + this.BTDevs[j].m_szName);
        }
      }
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
      i = -1;
    }
    return i;
  }

  int SaveCache()
  {
    int i = 0;
    Singleton.getInstance().Debug("BlueInput::BTInterface", "Save BTDevs " + this.BTCount);
    while (true)
    {
      int j = 0;
      try
      {
        ObjectOutputStream localObjectOutputStream = new ObjectOutputStream(new FileOutputStream("/data/data/com.teksoftco.android.blueinput/btcache.dat"));
        localObjectOutputStream.writeInt(this.BTCount);
        j = 0;
        if (j < this.BTCount)
          continue;
        localObjectOutputStream.close();
        //break label288;
        int k = this.BTDevs[j].m_szName.length();
        localObjectOutputStream.writeInt(k);
        localObjectOutputStream.write(this.BTDevs[j].m_szName.getBytes(), 0, k);
        int m = this.BTDevs[j].m_szAddress.length();
        localObjectOutputStream.writeInt(m);
        localObjectOutputStream.write(this.BTDevs[j].m_szAddress.getBytes(), 0, m);
        localObjectOutputStream.writeInt(this.BTDevs[j].m_nBTDEVType);
        if (this.BTDevs[j].m_lpRD == null)
          continue;
        localObjectOutputStream.writeInt(this.BTDevs[j].m_lpRD.length);
        Singleton.getInstance().Debug("BlueInput::BTInterface", "Save BTDevs RDlength(" + j + ")" + this.BTDevs[j].m_lpRD.length);
        localObjectOutputStream.write(this.BTDevs[j].m_lpRD, 0, this.BTDevs[j].m_lpRD.length);
        //break label290;
        localObjectOutputStream.writeInt(0);
      }
      catch (Exception localException)
      {
        localException.printStackTrace();
        i = -1;
      }
      //label288:
    	 // return i;
      //label290:
    	 j++;
    }
  }
}